home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: How to give functions as parameters?
- Date: 31 Jan 1996 00:13:07 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan30171307@qcd.lanl.gov>
- References: <Pine.SUN.3.91N2x.960130222756.11581A-100000@yellow59.nada.kth.se>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Per Steneskog's message of Tue, 30 Jan 1996 22:42:30 +0100
-
- In article
- <Pine.SUN.3.91N2x.960130222756.11581A-100000@yellow59.nada.kth.se> Per
- Steneskog <md94-pst@nada.kth.se> writes:
- <snip>
- I have a little problem, I dont know how to send a fuction,
- as a parameter to another function..
-
- Just as would you send a variable as a parameter to another function? You
- name it!
-
- void
- do_it(void (*it)())
-
- It is better style to declare it as void do_it(void (*it)(void))
- instead: always use a prototype if you can.
-
- <snip>
- void
- do_me(
- void)
- <snip>
- int
- main(
- void)
- {
- do_it(do_me()); /* ERROR-COMPILE LINE */
- }
-
- What are the () doing after the do_me? () means `call the
- previous and take the result'. You certainly do not want to pass the
- result of calling do_me! you want to pass do_me. So, just say
- do_it(do_me) ... heck, I would prefer different names :-)
-
- I do not understand why functions are mor confusing than arrays: I
- have never seen some one try to pass an array declared as int a[5] as
- `a[5]' or `a[]'. Why then the confusion about functions?
-
- By the way, if you think about the answer hard enough, you would
- discover another puzzle: You declare do_it as accepting a pointer to a
- function, and you pass it a function instead! It turns out that when a
- function (except in some illegal contexts) always `decays' to its
- address: so you could just as well have written do_it(&do_me). (Of
- course, it also implies that you can write
- (&*******do_it)(***&****&**do_me) if you really wanted to be
- perverse.) A related point is that you can declare a function as
- taking another function: the meaning is identical to the situation
- where you declare it as taking a pointer to a function.
-
- Short explaination: From main(), do_it() should be called with
- the parameter (and function) do_me(). (Pretty obvious :)
-
- When I try to compile this, i got the following error message: (gcc)
- In function `main':
- invalid use of void expression
-
- because you had asked the compiler to return the value of calling
- do_me to do_it, but do_me does not return a value!
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-